package com.mingye.components;

import com.mingye.board.Board;
import com.mingye.constants.LayoutConstants;
import com.mingye.listener.MoveKeyListener;

import javax.swing.*;
import java.awt.event.KeyListener;

/**
 * 主窗口
 *
 * @author daixinyi
 */
public class GameFrame extends JFrame {

    /**
     * 是否胜利
     */
    private boolean victoryFlag = false;

    /**
     * 键盘事件
     */
    private KeyListener keyListener;

    /**
     * 棋盘面板
     */
    private BoardPanel boardPanel;

    /**
     * 操作面板
     */
    private OperationPanel operationPanel;

    public GameFrame(Board board) {
        init(board);
    }

    /**
     * 初始化
     */
    private void init(Board board) {
        this.setSize(LayoutConstants.FRAME_WIDTH, LayoutConstants.FRAME_HEIGHT);
        this.setTitle("推箱子");
        this.setLocation(100, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        // 棋盘面板
        boardPanel = new BoardPanel(this, board);
        board.setBoardPanel(boardPanel);
        this.add(boardPanel);

        // 操作面板
        operationPanel = new OperationPanel(this);
        this.add(operationPanel);

        this.keyListener = new MoveKeyListener(this);
        this.addKeyListener(keyListener);
    }

    public BoardPanel getBoardPanel() {
        return boardPanel;
    }

    /**
     * 设置游戏胜利
     */
    public void victory() {
        JLabel winInfoLabel = operationPanel.getWinInfoLabel();
        winInfoLabel.setText("已过关！");
        victoryFlag = true;
    }

    public boolean isVictoryFlag() {
        return victoryFlag;
    }

    public void setVictoryFlag(boolean victoryFlag) {
        this.victoryFlag = victoryFlag;
    }

    public OperationPanel getOperationPanel() {
        return operationPanel;
    }
}
